package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;

@Repository("PackingmethodDAO")
@Transactional
public class PackingmethodDAOImpl extends AbstractJpaDao<Packingmethod> implements PackingmethodDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Packingmethod.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public PackingmethodDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Packingmethod findPackingmethodById(Integer id) throws DataAccessException {

		return findPackingmethodById(id, -1, -1);
	}

	@Transactional
	public Packingmethod findPackingmethodById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPackingmethodById", startResult, maxRows, id);
			return (Packingmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodByName(String name) throws DataAccessException {

		return findPackingmethodByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPackingmethodByName", startResult, maxRows, name);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodByNameContaining(String name) throws DataAccessException {

		return findPackingmethodByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPackingmethodByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodByFee(BigDecimal fee) throws DataAccessException {

		return findPackingmethodByFee(fee, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByFee(BigDecimal fee, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPackingmethodByFee", startResult, maxRows, fee);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodByDescription(String description) throws DataAccessException {

		return findPackingmethodByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPackingmethodByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodByDescriptionContaining(String description) throws DataAccessException {

		return findPackingmethodByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPackingmethodByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@Transactional
	public Packingmethod findPackingmethodByPrimaryKey(Integer id) throws DataAccessException {
		return findPackingmethodByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Packingmethod findPackingmethodByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPackingmethodByPrimaryKey", startResult, maxRows, id);
			return (Packingmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPackingmethodsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Packingmethod> findAllPackingmethods() throws DataAccessException {

		return findAllPackingmethods(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findAllPackingmethods(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPackingmethods", startResult, maxRows);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Packingmethod> findPackingmethodsByUnit(Integer unitId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllPackingmethodsByUnit", start, max, unitId);
		return new LinkedHashSet<Packingmethod>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Packingmethod> findPackingmethodByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Packingmethod>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Packingmethod> findPackingmethodsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Packingmethod.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Packingmethod> packingmethods = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return packingmethods;
	}

	@Transactional
	public Packingmethod findPackingmethodByPhysicalPrimaryKey(String name, BigDecimal fee, Integer unitId)
			throws DataAccessException {
		return findPackingmethodByPhysicalPrimaryKey(name, fee, unitId, -1, -1);
	}

	@Transactional
	public Packingmethod findPackingmethodByPhysicalPrimaryKey(String name, BigDecimal fee, Integer unitId,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPackingmethodByPhyscialPrimaryKey", startResult, maxRows, name, fee,
					unitId);
			return (Packingmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Packingmethod entity) {
		return true;
	}
}
